package com.example.transportservice;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

@SuppressLint("NewApi") public class LightInfoActivity extends Fragment {
	private View view;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.activity_traffic_light, container,false);	
		new TrafficLightActivity().execute("1");
		new TrafficLightActivity().execute("2");
		new TrafficLightActivity().execute("3");
		new TrafficLightActivity().execute("4");
		new TrafficLightActivity().execute("5");
		return view;
	}
	public class TrafficLightActivity extends AsyncTask<String, Void, String> {
		int lightId;
		@Override
		protected String doInBackground(String... strings) {
			lightId = Integer.valueOf(strings[0]);
			String info;
			try {
				info = new GetLightInfo().getLightInfo(lightId);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				return "NO";
			} catch (JSONException e) {
				return "NO!";
			}
			return info;
		}

		@Override
		protected void onPostExecute(String result) {
//			System.out.println(result);
			if (lightId == 1) {
				TextView red = (TextView) view.findViewById(R.id.Redlight1);
				TextView green = (TextView) view.findViewById(R.id.greenlight1);
				TextView yellow = (TextView) view.findViewById(R.id.yellowlight1);
				try {
					String RedTime = new JSONObject(result)
							.getString("RedTime");
					red.setText("RedTime:" + RedTime);
					String GreenTime = new JSONObject(result)
							.getString("GreenTime");
					green.setText("GreenTime:" + GreenTime);
					String YellowTime = new JSONObject(result)
							.getString("YellowTime");
					yellow.setText("Yellow:" + YellowTime);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (lightId == 2) {
				TextView red = (TextView) view.findViewById(R.id.Redlight1);
				TextView green = (TextView) view.findViewById(R.id.greenlight1);
				TextView yellow = (TextView) view.findViewById(R.id.yellowlight1);
				try {
					String RedTime = new JSONObject(result)
						.getString("RedTime");
					red.setText("Red:" + RedTime+"s");
					String GreenTime = new JSONObject(result)
						.getString("GreenTime");
					green.setText("Green:" + GreenTime+"s");
					String YellowTime = new JSONObject(result)
						.getString("YellowTime");
					yellow.setText("Yellow:" + YellowTime+"s");
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (lightId == 2) {
				TextView red = (TextView) view.findViewById(R.id.redlight2);
				TextView green = (TextView) view.findViewById(R.id.greenlight2);
				TextView yellow = (TextView) view.findViewById(R.id.yellowlight2);
				try {
					String RedTime = new JSONObject(result)
						.getString("RedTime");
					red.setText("Red:" + RedTime+"s");
					String GreenTime = new JSONObject(result)
						.getString("GreenTime");
					green.setText("Green:" + GreenTime+"s");
					String YellowTime = new JSONObject(result)
						.getString("YellowTime");
					yellow.setText("Yellow:" + YellowTime+"s");
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (lightId == 3) {
				TextView red = (TextView) view.findViewById(R.id.redlight3);
				TextView green = (TextView) view.findViewById(R.id.greenlight3);
				TextView yellow = (TextView) view.findViewById(R.id.yellowlight3);
				try {
					String RedTime = new JSONObject(result)
						.getString("RedTime");
					red.setText("Red:" + RedTime+"s");
					String GreenTime = new JSONObject(result)
						.getString("GreenTime");
					green.setText("Green:" + GreenTime+"s");
					String YellowTime = new JSONObject(result)
						.getString("YellowTime");
					yellow.setText("Yellow:" + YellowTime+"s");
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (lightId == 4) {
				TextView red = (TextView) view.findViewById(R.id.redlight4);
				TextView green = (TextView) view.findViewById(R.id.greenlight4);
				TextView yellow = (TextView) view.findViewById(R.id.yellowlight4);
				try {
					String RedTime = new JSONObject(result)
							.getString("RedTime");
					red.setText("Red:" + RedTime+"s");
					String GreenTime = new JSONObject(result)
							.getString("GreenTime");
					green.setText("Green:" + GreenTime+"s");
					String YellowTime = new JSONObject(result)
							.getString("YellowTime");
					yellow.setText("Yellow:" + YellowTime+"s");
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (lightId == 5) {
				TextView red = (TextView) view.findViewById(R.id.redlight5);
				TextView green = (TextView) view.findViewById(R.id.greenlight5);
				TextView yellow = (TextView) view.findViewById(R.id.yellowlight5);
				try {
					String RedTime = new JSONObject(result)
							.getString("RedTime");
					red.setText("Red:" + RedTime+"s");
					String GreenTime = new JSONObject(result)
							.getString("GreenTime");
					green.setText("Green:" + GreenTime+"s");
					String YellowTime = new JSONObject(result)
							.getString("YellowTime");
					yellow.setText("Yellow:" + YellowTime+"s");
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}

	}


//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.light_info, menu);
//		return true;
//	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
